<?php

class ValidatorPeer //extends BasePeer
{
	/**
	 * Applies any validators that were defined in the schema to the specified columns.
	 *
	 * @param      string $dbName The name of the database
	 * @param      string $tableName The name of the table
	 * @param      array $columns Array of column names as key and column values as value.
	 */
	public static function doValidate($dbName, $tableName, $columns, $obj=null)
	{
	
	
		$dbMap = Propel::getDatabaseMap($dbName);
		$tableMap = $dbMap->getTable($tableName);
		$failureMap = array(); // map of ValidationFailed objects
		foreach ($columns as $colName => $colValue) 
        {
        
			if ($tableMap->containsColumn($colName)) 
            {
            	$col = $tableMap->getColumn($colName);
				foreach ($col->getValidators() as $validatorMap) 
                {
					$validator = BasePeer::getValidator($validatorMap->getClass());
					if(method_exists($validator, "setColumns"))
					{
                     $validator->setColumns($columns);
                     $validator->setObject($obj);
                    }
					if ($validator && ($col->isNotNull() || $colValue !== null) && $validator->isValid($validatorMap, $colValue) === false) 
                    {  
                    	if (!isset($failureMap[$colName])) { // for now we do one ValidationFailed per column, not per rule
							$failureMap[$colName] = new ValidationFailed($colName, $validatorMap->getMessage(), $validator);
						}
					}
				}
			}
		}
		return (!empty($failureMap) ? $failureMap : true);
	}


}
